package com.itheima.health.mobile.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.common.ValidateCodeType;
import com.itheima.health.entity.Result;
import com.itheima.health.exception.DuplicateOperateException;
import com.itheima.health.mobile.vo.SmsLoginParam;
import com.itheima.health.pojo.Member;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.SmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/mobile/login")
@Slf4j
public class LoginController {

    @Reference
    private SmsService smsService;
    @Reference
    private MemberService memberService;
    @RequestMapping("/smsLogin")
    public Result login(@RequestBody SmsLoginParam smsLoginParam){
        boolean success = smsService.checkValidateCode(ValidateCodeType.OMS_SIGNIN, smsLoginParam.getTelephone(), smsLoginParam.getValidateCode());
        if (!success){
            return new Result(false, MessageConst.VALIDATECODE_ERROR);
        }
        Member member = new Member();
        member.setRegTime(new Date());
        member.setPhoneNumber(smsLoginParam.getTelephone());
        try {
            memberService.add(member);
        } catch (DuplicateOperateException e) {
            log.info("用户已存在",e);
        }
        log.info("[登录成功]》》》》》》》》》》》》》》》》》》》》:{}",member.getId());
        return new Result(true,MessageConst.LOGIN_SUCCESS);
    }
}
